package com.hzsd.zengbanyi.biaodan_text.Actrivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzsd.zengbanyi.biaodan_text.BaseActivity;
import com.hzsd.zengbanyi.biaodan_text.MyAdapter.DetailListAdapter;
import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.MoreBean;
import com.hzsd.zengbanyi.biaodan_text.bean.Sample;
import com.hzsd.zengbanyi.biaodan_text.bean.WorkDAO;
import com.hzsd.zengbanyi.biaodan_text.fragment.PollutionSceneFragment;
import com.hzsd.zengbanyi.biaodan_text.fragment.SurfaceWaterSceneFragment;

import java.util.ArrayList;
import java.util.HashMap;


public class DetailListActivity extends BaseActivity {
    private ListView listView;
    private TextView textTitle;
    private WorkDAO taskDAO;
    private RelativeLayout rl_return;
    private TextView btReturn;
    private MoreBean moreBean;

    private String title, taskId, taskSceneId;
    private ArrayList<Sample> sampleList;
    private DetailListAdapter detailListAdapter;

    private String type;

    private PollutionSceneFragment pollutionSceneFragment;
    private SurfaceWaterSceneFragment surfaceWaterSceneFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_list);
        initBaseData();
        initView();
        initEvents();
        category();
        initDB();
    }

    private void initBaseData() {
        Bundle bundle = getIntent().getExtras();
        type = bundle.getString("type");

        SharedPreferences share = getSharedPreferences("user", MODE_PRIVATE);
        title = share.getString("title", "");
        taskId = share.getString("taskId", "");
        taskSceneId = share.getString("taskSceneId", "");
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.listview);
        textTitle = (TextView) findViewById(R.id.tv_title);
        rl_return = (RelativeLayout) findViewById(R.id.rl_return);
    }

    private void initEvents() {
        textTitle.setText(title);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent();
                String entityName = taskDAO.getEntityName(taskId);
//				if (entityName.contains("水")) {
                intent = new Intent(DetailListActivity.this, EditSampleActivity.class);
//				}
//				else {
//					intent = new Intent(DetailListActivity.this, EditGasSampleActivity.class);
//				}
                Bundle bundle = new Bundle();
                bundle.putString("type", type);
                intent.putExtras(bundle);
                SharedPreferences sp = getSharedPreferences("user",
                        MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit(); // 编辑文件
                edit.putString("samplePlace",
                        sampleList.get(position).samplePlace);
                edit.putString("sampleCode",
                        sampleList.get(position).sampleCode);
                edit.putString("sampleGeneralPlace",
                        sampleList.get(position).sampleGeneralPlace);
                edit.putString("sampleId", sampleList.get(position).sampleId);
                edit.putString("position", position + "");
                edit.commit(); // 保存数据信息

                // startActivity(intent);
                startActivityForResult(intent, 1);
            }
        });

        rl_return.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });

    }

    private void category() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        switch (type) {
            case "污染源":
                pollutionSceneFragment = new PollutionSceneFragment();
                ft.replace(R.id.fl_fragment, pollutionSceneFragment);
                ft.commit();
                break;
            case "环境质量":// 地表水
                surfaceWaterSceneFragment = new SurfaceWaterSceneFragment();
                ft.replace(R.id.fl_fragment, surfaceWaterSceneFragment);
                ft.commit();
                break;

            default:
                break;
        }

    }

    private void initDB() {
        taskDAO = new WorkDAO(this);
        sampleList = taskDAO.getSimple(taskId, taskSceneId);
        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < sampleList.size(); i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("sampleGeneralPlace", sampleList.get(i).sampleGeneralPlace
                    + "【" + sampleList.get(i).samplePlace + "】");
            map.put("sampleId", sampleList.get(i).sampleId);
            map.put("sampleCode", sampleList.get(i).sampleCode);
            data.add(map);
        }
        detailListAdapter = new DetailListAdapter(DetailListActivity.this, data);
        listView.setAdapter(detailListAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            detailListAdapter.notifyDataSetChanged();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
